(ns fx-clj.internal.util
  (:require [clojure.reflect :as r]
            [clojure.string :as str]))

(def ^:private debug? true)

(def start-timestamp (delay
                       (println "Start loading...")
                       (System/currentTimeMillis)))
(def the-end (atom 0))

(def time-records (atom {}))

(defn ns-start-loading [sym]
  (when debug?
    @start-timestamp
    (swap! time-records assoc sym
           {:start (- (System/currentTimeMillis) @start-timestamp)})))

(defn ns-libs-loaded [sym]
  (when debug?
    (let [t (- (System/currentTimeMillis) @start-timestamp)]
      (swap! time-records update sym
             (fn [init]
               (when init
                 (assoc init :libs t)))))))

(defn ns-loaded [sym]
  (when debug?
    (let [st (get-in @time-records [sym :start] 0)
          et (- (System/currentTimeMillis) @start-timestamp)]
      (reset! the-end et)
      (swap! time-records update sym
             (fn [init]
               (when init
                 (assoc init :end et))))
      (println sym "loaded." (- et st) "ms"))))

